package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.nursing.dto.HandlaAlertDataDto;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.service.IAlertDataService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
/**
 * 报警数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-25
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper,AlertData> implements IAlertDataService
{

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertDataDto 报警数据
     * @return 报警数据
     */
    @Override
    public TableDataInfo<AlertData> selectAlertDataList(AlertDataDto alertDataDto)
    {
        IPage<AlertData> page = Page.of(alertDataDto.getPageNum(), alertDataDto.getPageSize());
        Long userId = SecurityUtils.getLoginUser().getUserId();
        lambdaQuery().eq(StrUtil.isNotEmpty(alertDataDto.getDeviceName()), AlertData::getDeviceName, alertDataDto.getDeviceName()).or(w -> w.eq(AlertData::getProcessorId, userId).or().eq(AlertData::getUserId, userId)).eq(ObjectUtil.isNotEmpty(alertDataDto.getStatus()), AlertData::getStatus, alertDataDto.getStatus()).between(ObjectUtil.isNotEmpty(alertDataDto.getStartTime()), AlertData::getCreateTime, alertDataDto.getStartTime(), alertDataDto.getEndTime()).page(page);
        return new TableDataInfo(page.getRecords(), page.getTotal());
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData)?1:0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData)?1:0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids))?1:0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id)?1:0;
    }

    @Override
    public void handlaAlertData(HandlaAlertDataDto handlaAlertDataDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        lambdaUpdate()
                .set(AlertData::getProcessorId, loginUser.getUserId())
                .set(AlertData::getProcessorName,loginUser.getUser().getNickName())
                .set(AlertData::getProcessingResult, handlaAlertDataDto.getProcessingResult())
                .set(AlertData::getStatus, 1)
                .set(AlertData::getProcessingTime, handlaAlertDataDto.getProcessingTime())
                .eq(AlertData::getId, handlaAlertDataDto.getId()).update();
    }
}
